var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var user = require('../../utils/user.js');
var check = require('../../utils/check.js');
// pages/upinfo/upinfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zz:[],
    zz1:[],
    name:"",
    man: "",
    address: "",
    desc: "",
    leumu: "",
    tel: "",
    imgList:[],
    imgList1: [],
    index: '',
    picker: ['蔬菜', '水果', '粮油副食','冻肉','海鲜','禽类','鱼类','母婴','化妆'],
  },
  name(e) {
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },

  man(e) {
    console.log(e)
    this.setData({
      man: e.detail.value
    })
  },

  address(e) {
    console.log(e)
    this.setData({
      address: e.detail.value
    })
  },

  desc(e) {
    console.log(e)
    this.setData({
      desc: e.detail.value
    })
  },

  leimu(e) {
    console.log(e)
    this.setData({
      leimu: e.detail.value
    })
  },
  

  tel(e) {
    console.log(e)
    this.setData({
      tel: e.detail.value
    })
  },





  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },
  ChooseImage() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        console.log(res.tempFilePaths)
        let that=this
        for (var i = 0; i < res.tempFilePaths.length;i++){
        wx.uploadFile({
          url: api.MultipleSave, //仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[i],
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data"
          },
          formData: {
            'user': 'test'
          },
          success(res) {
            console.log(res.data)
            var bb = JSON.stringify(res.data)
            console.log(bb.data)
            var sstr = bb.split("\"");
            var gg = sstr[sstr.length - 3]
           gg = gg.substring(0, gg.length - 1);  
            console.log(gg)
            //do something
           let zz=that.data.zz
           zz.push(gg)
           that.setData({
             zz:zz
           })
          }
        })
       
      }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '操作提示',
      content: '确定要删除吗？',
      cancelText: '再看看',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },





  ChooseImage1() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList1.length != 0) {
          this.setData({
            imgList1: this.data.imgList1.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList1: res.tempFilePaths
          })
        }
        let that =this
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          wx.uploadFile({
            url: api.MultipleSave, //仅为示例，非真实的接口地址
            filePath: res.tempFilePaths[i],
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: {
              'user': 'test'
            },
            success(res) {
              console.log(res.data)
              var bb = JSON.stringify(res.data)
              console.log(bb.data)
              var sstr = bb.split("\"");
              var gg = sstr[sstr.length - 3]
              gg = gg.substring(0, gg.length - 1);
              console.log(gg)
              //do something
              let zz1 = that.data.zz1
              zz1.push(gg)
              that.setData({
                zz1: zz1
              })
            }
          })

        }
      }
    });
  },
  ViewImage1(e) {
    wx.previewImage({
      urls: this.data.imgList1,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg1(e) {
    wx.showModal({
      title: '操作提示',
      content: '确定要删除吗？',
      cancelText: '再看看',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          this.data.imgList1.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList1: this.data.imgList1
          })
        }
      }
    })
  },

 tijiao(){
   console.log(this.data.zz)
   console.log(this.data.zz1)
   if(this.data.name==''){
     wx.showToast({
       title: '请填写商家名称',
       icon:'none'
     })
     return false
   }
   if (this.data.man == '') {
     wx.showToast({
       title: '请填写联系人',
       icon: 'none'
     })
     return false
   }
   if (this.data.name == '') {
     wx.showToast({
       title: '请填写详细地址',
       icon: 'none'
     })
     return false
   }
   if (this.data.name == '') {
     wx.showToast({
       title: '请填写商家简介',
       icon: 'none'
     })
     return false
   }
   if (this.data.index == '') {
     wx.showToast({
       title: '请选择分类',
       icon: 'none'
     })
     return false
   }
   if (this.data.tel == '') {
     wx.showToast({
       title: '请填写联系方式',
       icon: 'none'
     })
     return false
   }
  
   if (this.data.zz.length==0) {
     wx.showToast({
       title: '上传营业执照',
       icon: 'none'
     })
     return false
   }
   if (this.data.zz1.length == 0) {
     wx.showToast({
       title: '请上传商品图片',
       icon: 'none'
     })
     return false
   }
   if (!check.isValidPhone(this.data.tel)) {
     wx.showModal({
       title: '错误信息',
       content: '手机号输入不正确',
       showCancel: false
     });
     return false;
   }
     let that = this
   let bb = that.data.picker[that.data.index]
   const aa=[]
   aa.push(bb)
   util.request(api.AddMerchantInfo,JSON.stringify( {
     address: that.data.address,
     introduction:that.data.desc,
     licenseInfo: String(that.data.zz),
     mainSale: JSON.stringify(aa),
     name: that.data.name,
     phone: that.data.tel,
     storeImg: JSON.stringify(that.data.zz1),
     touchMan: that.data.man
   }), 'POST', {
       'Content-Type': 'application/json;charset=UTF-8',
       'token': wx.getStorageSync('token')
       }).then(function (res) {
         console.log(res)
         if (res.code==200) {
           wx.showModal({
             title: '申请成功',
             content: '申请成功，亲等待我们的审核，通过后会立即联系你',
             showCancel: false,
             success: res => {
               wx.navigateTo({
                 url: '/pages/index/index',
               })
             }
           });
           return false;
         }
         if (res.code != 200) {
           wx.showToast({
             title: res.msg,
             icon: 'none'
           })
         }
       });
  

 },
  
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    user.checkLogin().then(res => {
    }).catch(() => {
      wx.clearStorageSync("token")
      wx.showModal({
        title: '温馨提示',
        content: '登录过期或未登录',
        showCancel: false,
        success: res => {
          wx.redirectTo({
            url: '/pages/login/login',
          })
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})